package com.example.lib_widget.redpacket;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.lib_widget.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * @author yaotianxue
 * @description
 * @date 2024/10/26
 *
 * 1.使用EventBus解耦
 * 2.上报金币动画
 */
public class RedPacketWidget extends LinearLayout {
    private TextView tvNum; //上面+100 文字
    private RedPacketView redPacketView; //红包
    private TranslateAnimation translateAnimation; //平移动画

    public RedPacketWidget(Context context) {
        super(context);
        initView(context);
    }
    public RedPacketWidget(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);

    }

    public RedPacketWidget(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public RedPacketWidget(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }
    private void initView(Context context) {
        removeAllViews();
        View view = LayoutInflater.from(context).inflate(R.layout.layout_redpacketwidget, null, false);
        tvNum = view.findViewById(R.id.tv_redpacket_num);
        redPacketView = view.findViewById(R.id.rpv_redpacket);
        addView(view);
        //GONE：不可见，不会占位置  INVISIBLE：不可见，占位置  VISIBEL：可见
        tvNum.setVisibility(INVISIBLE);
        //动画。。。。。。。。。。。
        //补间动画和属性动画区别？
        //补间动画Animation：默认不会保持最后的状态  属性动画Animator：保持最后的状态
        translateAnimation = new TranslateAnimation(0.0F,0.0F,0.0F,-70.0F);
        translateAnimation.setFillAfter(false);//保持最后的状态
        //动画执行时间
        translateAnimation.setDuration(2000);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                tvNum.setVisibility(VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                tvNum.setVisibility(INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        //注册EventBus
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        //注销EventBus
        EventBus.getDefault().unregister(this);
        //停止
        redPacketView.stop();
    }
    /**
     * EventBus 事件处理
     * @param event
     */
    @Subscribe
    public void onEvent(RedPacketEvent event){
        switch (event.getState()){
            case INIT:
                start();
                break;
            case RESUME:
                redPacketView.resume();
                break;
            case PAUSE:
                redPacketView.pause();
                break;
        }
    }

    public void start(){
        redPacketView.startAnimator();
        redPacketView.setCallback(new RedPacketView.Callback() {
            @Override
            public void start() {

            }

            @Override
            public void pause(int angle) {

            }

            @Override
            public void resume() {

            }

            @Override
            public void completed() {
                tvNum.startAnimation(translateAnimation);
                redPacketView.startAnimator();//重新继续
                if (stateCallback!= null){//接口回调fragment上报金币
                    stateCallback.completed();
                }
            }
        });
    }
    /**
     * 设置文本提示信息
     * @param num
     */
    public void setTvNum(int num){
        tvNum.setText("+"+num);
    }


    private StateCallback stateCallback;

    public void setStateCallback(StateCallback stateCallback) {
        this.stateCallback = stateCallback;
    }

    /**
     * 状态回调接口
     */
    public interface StateCallback{
        void completed();
    }



}
